#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"inc/app.h"
//#include"lv_mygec_font.h"
#include"lv_conf.h"
#include"inc/app.h"
#include"inc/game.h"
#include"inc/tem.h"
#include"inc/pthread.h"    
#include"inc/game2048.h"
//int  tem_ui_requested ;
 lv_obj_t*  app_container;
//lv_obj_t*  src;
extern const lv_font_t lv_mygec_font;
lv_obj_t* create_app_button(lv_obj_t* parent, const char* icon_path, const char* app_name, int x, int y) 
{
    // 按钮容器
      lv_obj_t* btn1 = lv_btn_create(parent);
    lv_obj_set_pos(btn1, x, y);
    lv_obj_set_size(btn1, 100, 120);
    
    // 按钮样式
    lv_obj_set_style_bg_color(btn1, lv_color_hex(0x34495e), 0);
    lv_obj_set_style_bg_color(btn1, lv_color_hex(0x4a6572), LV_STATE_PRESSED);
    lv_obj_set_style_shadow_color(btn1, lv_color_hex(0x1a2530), 0);
    lv_obj_set_style_border_width(btn1, 0, 0);
    
    // 应用图标 (80x80)
    lv_obj_t* icon1 = lv_img_create(btn1);
    lv_img_set_src(icon1, icon_path);
    
    
    // 应用名称标签 (100x30)
    lv_obj_t* label1 = lv_label_create(btn1);
    lv_label_set_text(label1, app_name);
    lv_obj_set_width(label1, 100); // 设置标签宽度
    lv_obj_set_style_text_font(label1, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(label1, lv_color_white(), 0);
    lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0); // 文本居中
    lv_obj_align(label1, LV_ALIGN_BOTTOM_MID, 0, -1);
  
    return btn1;
}

  
void game_op(lv_event_t *e)//五子棋操作
{    
    // 隐藏软件盒容器
   
        lv_obj_add_flag(app_container, LV_OBJ_FLAG_HIDDEN);
    
    
    // 确保之前的线程已结束
    if(tid1) 
    {
        pthread_join(tid1, NULL);
        tid1 = 0;
    }
    
    int ret = pthread_create(&tid1, NULL, game, NULL);
    if(ret != 0) 
    {
        perror("pthread_create error");
    }
     //lv_refr_now(NULL);
}

void game2_op(lv_event_t *e)//2048操作
{    
  /// 隐藏软件盒容器
   
        lv_obj_add_flag(app_container, LV_OBJ_FLAG_HIDDEN);
    
      
    // 确保之前的线程已结束
    if(tid2) 
    {
        pthread_join(tid2, NULL);
        tid2 = 0;
    }
    
    int ret2 = pthread_create(&tid2, NULL, game2, NULL);
    if(ret2 != 0) 
    {
        perror("pthread_create error");
    }
}
void tem_op(lv_event_t *e)//温湿度检测操作
{    
  // pthread_t tid3;
   // 隐藏软件盒容器
   
  lv_obj_add_flag(app_container, LV_OBJ_FLAG_HIDDEN);

    create_tem(screen);
     
    if(tid3) 
    {
        pthread_join(tid3, NULL);
        tid3= 0;
    }
    
    int ret3 = pthread_create(&tid3, NULL, (void *(*)(void *))read_sensor_data, NULL);
    if(ret3 != 0) 
    {
        perror("pthread_create error");
    }
}

void exit_app(lv_event_t *e)//退出
{    
  lv_obj_add_flag(app_container, LV_OBJ_FLAG_HIDDEN);
    lv_obj_remove_flag(btn, LV_OBJ_FLAG_HIDDEN); 
  //exit(0);
}

void create_app(lv_obj_t* parent)//软件盒界面
{
  /*lv_style_t style_obj;
    lv_style_init(&style_obj); 
    // 面板背景
    lv_obj_t*panel2 = lv_obj_create(parent);
    lv_obj_set_size(panel2, 800, 480);
    lv_obj_set_pos(panel2, 0, 0);*/
    
    // 创建容器来包装所有应用按钮
    app_container = lv_obj_create(parent);
    lv_obj_set_size(app_container, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
    lv_obj_set_align(app_container, LV_ALIGN_TOP_LEFT);
    lv_obj_clear_flag(app_container, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(app_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(app_container, 0, 0);

    lv_obj_t* game_btn=create_app_button(app_container,"A:/tzh/game.bmp", "五子棋", 80, 30);
    lv_obj_add_event_cb(game_btn,game_op,LV_EVENT_CLICKED,NULL);

    lv_obj_t* game2_btn=create_app_button(app_container,"A:/tzh/game2048.bmp", "2048",80*2+100, 30);
    lv_obj_add_event_cb(game2_btn,game2_op,LV_EVENT_CLICKED,NULL);

     lv_obj_t* tem_btn=create_app_button(app_container,"A:/tzh/tem.bmp", "环境监测", 80*3+100*2, 30);
    lv_obj_add_event_cb(tem_btn,tem_op,LV_EVENT_CLICKED,NULL);
     pthread_mutex_init(&mutex,NULL);


     lv_obj_t* exit_btn = lv_btn_create(app_container);
    lv_obj_set_pos(exit_btn, 600, 350);
    lv_obj_set_size(exit_btn, 120, 100);
   
     lv_obj_t* exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, "退出");
    lv_obj_add_event_cb(exit_btn,exit_app,LV_EVENT_CLICKED,NULL);
     lv_obj_set_style_text_font(exit_label, &lv_mygec_font, LV_PART_MAIN);
    // tem_ui_requested = 0;
}